﻿const ATTACK_VALUE=10;
const STRONG_ATTACK_VALUE = 17;
const MONSTER_ATTACK_VALUE = 14;
const HEAL_VALUE = 20;

const MODE_ATTACK ='ATTACk';
const MODE_STRONG_ATTACK = 'STRONG_ATTACK';

const LOG_EVENT_PLAYER_ATTACK = 'PLAYER_ATTACK';
const LOG_EVENT_PLAYER_STRONG_ATTACK ='PLAYER_STRONG_ATTACK';
const LOG_EVENT_MONSTER_ATTACK='MONSTER_ATTACK';
const LOG_EVENT_PLAYER_HEAL='PLAYER_HEAL';
const LOG_EVENT_GAME_OVER = 'GAME_OVER';
let battleLog = [];
let lastLoggerEntry;

function getMaxLifeValues(){
    const enteredValue = prompt('Maximum life for you and the monster.','100');

    const parsedValue = parseInt(enteredValue);
    if(isNaN(parsedValue) || parsedValue <= 0){
        throw{message:'Invalid user input,not a number'};
    }
    return parsedValue;
}

let chosenMaxLife;

try{
    chosenMaxLife = getMaxLifeValues();
}catch(error){
    console.log(error);
    chosenMaxLife = 100;
    alert('You entered something wrong,default value of 100 was used.');
}


let currentMonsterHealth = chosenMaxLife;
let currentPlayerHealth = chosenMaxLife;
let hasBonusLife = true;

adjustHealthBars(chosenMaxLife);

function writeToLog(ev,val,monsterHealth,playerHealth){
    let logEnter={
        event:ev,
        val:val,
        finalMonsterHealth:monsterHealth,
        finalPlayerHealth:playerHealth
    };
    switch(ev){
        case LOG_EVENT_PLAYER_ATTACK:
            logEnter.target = 'MONSTER';
            break;
        case LOG_EVENT_PLAYER_STRONG_ATTACK:
            logEnter={
                event:ev,
                val:val,
                target:'MONSTER',
                finalMonsterHealth:monsterHealth,
                finalPlayerHealth:playerHealth
            };
            break;
        case LOG_EVENT_MONSTER_ATTACK:
            logEnter={
                event:ev,
                val:val,
                target:'PLAYER',
                finalMonsterHealth:monsterHealth,
                finalPlayerHealth:playerHealth
            };
            break;
        case LOG_EVENT_PLAYER_HEAL:
            logEnter={
                event:ev,
                val:val,
                target:'PLAYER',
                finalMonsterHealth:monsterHealth,
                finalPlayerHealth:playerHealth
            };
            break;
        case LOG_EVENT_GAME_OVER:
            logEnter={
                event:ev,
                val:val,
                finalMonsterHealth:monsterHealth,
                finalPlayerHealth:playerHealth
            };
            break;
        default:
            logEnter={};
    }
    battleLog.push(logEnter);

    // if(ev === LOG_EVENT_PLAYER_ATTACK){
    //     logEnter={
    //         event:ev,
    //         val:val,
    //         target:'MONSTER',
    //         finalMonsterHealth:monsterHealth,
    //         finalPlayerHealth:playerHealth
    //     };
    // }else if(ev === LOG_EVENT_PLAYER_STRONG_ATTACK){
    //     logEnter={
    //         event:ev,
    //         val:val,
    //         target:'MONSTER',
    //         finalMonsterHealth:monsterHealth,
    //         finalPlayerHealth:playerHealth
    //     };
    // }else if(ev === LOG_EVENT_MONSTER_ATTACK) {
    //     logEnter={
    //         event:ev,
    //         val:val,
    //         target:'PLAYER',
    //         finalMonsterHealth:monsterHealth,
    //         finalPlayerHealth:playerHealth
    //     };
    // }else if(ev === LOG_EVENT_PLAYER_HEAL){
    //     logEnter={
    //         event:ev,
    //         val:val,
    //         target:'PLAYER',
    //         finalMonsterHealth:monsterHealth,
    //         finalPlayerHealth:playerHealth
    //     };
    // }else if(ev ===LOG_EVENT_GAME_OVER){
    //     logEnter={
    //         event:ev,
    //         val:val,
    //         finalMonsterHealth:monsterHealth,
    //         finalPlayerHealth:playerHealth
    //     };
    // }
    //battleLog.push(logEnter);
}

function reset(){
    currentMonsterHealth = chosenMaxLife;
    currentPlayerHealth = chosenMaxLife;
    resetGame(chosenMaxLife);
}

function endRound(){
    const initialPlayerLife = currentMonsterHealth;
    const playerDamage = dealPlayerDamage(MONSTER_ATTACK_VALUE);
    currentPlayerHealth -= playerDamage;
    writeToLog(LOG_EVENT_MONSTER_ATTACK,playerDamage,currentMonsterHealth,currentPlayerHealth);

    if(currentPlayerHealth <= 0 && hasBonusLife){
        hasBonusLife = false;
        removeBonusLife();
        currentPlayerHealth = initialPlayerLife;
        setPlayerHealth(initialPlayerLife);
        alert('You would be dead but the bonus life saved you!');
    }

    if(currentMonsterHealth <= 0 && currentPlayerHealth > 0 ){
        alert('You won');
        writeToLog(LOG_EVENT_GAME_OVER,'PLAYER WON',currentMonsterHealth,currentPlayerHealth);
    }else if(currentPlayerHealth <= 0 && currentMonsterHealth > 0){
        alert('You lost');
        writeToLog(LOG_EVENT_GAME_OVER,'MONSTER WON',currentMonsterHealth,currentPlayerHealth);
    }else if(currentPlayerHealth <= 0 && currentMonsterHealth <= 0){
        alert('You have a draw');
        writeToLog(LOG_EVENT_GAME_OVER,'A DRAW',currentMonsterHealth,currentPlayerHealth);
    }
    if(currentMonsterHealth <= 0 || currentPlayerHealth <= 0){
        reset();
    }
}

function attackMonster(mode){
    const maxDamage = mode === MODE_ATTACK ? ATTACK_VALUE : STRONG_ATTACK_VALUE;
    const logEvent = mode === MODE_ATTACK? LOG_EVENT_PLAYER_ATTACK:LOG_EVENT_PLAYER_STRONG_ATTACK;

    // if(mode===MODE_ATTACK){
    //     maxDamage =  dealMonsterDamage(ATTACK_VALUE);
    //     logEvent = LOG_EVENT_PLAYER_ATTACK;
    // }else if(mode === MODE_STRONG_ATTACK){
    //     maxDamage =  dealMonsterDamage(STRONG_ATTACK_VALUE);
    //     logEvent= LOG_EVENT_PLAYER_STRONG_ATTACK;
    // }
    const damage = dealMonsterDamage(maxDamage);
    currentMonsterHealth -= damage;
    writeToLog(logEvent,damage,currentMonsterHealth,currentPlayerHealth);
    endRound();
}

function attackHandler(){
    attackMonster(MODE_ATTACK);
}

function  strongAttackHandler(){
    attackMonster(MODE_STRONG_ATTACK);
}

function healPlayerHandler(){
    let healValue;
    if(currentPlayerHealth >= chosenMaxLife - HEAL_VALUE){
        healValue = chosenMaxLife - currentPlayerHealth;
    }else{
        healValue = HEAL_VALUE;
    }
    increasePlayerHealth(healValue);
    currentPlayerHealth += healValue;
    writeToLog(LOG_EVENT_PLAYER_HEAL,healValue,currentMonsterHealth,currentPlayerHealth);
    endRound();
}

function printLogHandler(){
    console.log(battleLog);
}

attackBtn.addEventListener("click", attackHandler);
strongAttackBtn.addEventListener('click',strongAttackHandler);
healBtn.addEventListener('click',healPlayerHandler);
logBtn.addEventListener('click',printLogHandler);
